package com.example.droidwarehouse;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.droidwarehouse.item.Item;
import com.example.droidwarehouse.item.ItemService;

/**
 * Activity for registration of a new warehouse item
 * 
 * @author Bibek Shrestha <bibek.shrestha@tum.de>
 * @author David Schattel
 * @author Dansen Zhou
 * @since 15.11.2012
 */
public class RegisterActivity extends Activity
{
    private Button b_save;
    private Button b_cancel;
    private TextView e_itemID;
    private EditText e_itemName;

    private final Item item = new Item();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity);

        Bundle bundle = getIntent().getExtras();
        String itemId = bundle.getString("itemId");

        e_itemID = (TextView) findViewById(R.id.e_itemID);
        e_itemID.setText(itemId);
        item.setId(itemId);

        e_itemName = (EditText) findViewById(R.id.e_itemName);

        b_cancel = (Button) findViewById(R.id.b_cancel);
        b_cancel.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent();
                intent.setClass(RegisterActivity.this, MainActivity.class);
                startActivity(intent);
                RegisterActivity.this.finish();
            }
        });

        b_save = (Button) findViewById(R.id.b_save_register);
        b_save.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO
                saveRegister();
            }

        });
    }

    /**
     * Collects the values from UI and calls the ItemService to mark the item
     * from unknown to available
     */
    private void saveRegister()
    {
        item.setName(e_itemName.getText().toString());

        ItemService service = new ItemService();
        service.register(item);

        // back to main activity
        Intent intent = new Intent();
        intent.setClass(RegisterActivity.this, MainActivity.class);
        startActivity(intent);
        RegisterActivity.this.finish();
    }
}
